﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace DukeNUS.FacilitiesPlanning.FPSystem
{

    public enum ApplicationErrorMessages
    {
        ActionNotAllowed = 3,
        InvalidLogonCredentials = 2,
        InvalidPassword = 5,
        MultipleLoginAttempt  = 1,
        PasswordsDoesNotMatch = 4
    }

    /// <summary>
    /// The application's reference for error messages. See 'ErrorMessages' key in the Web.config file for the list of stored error messages.
    /// </summary>
    public abstract class ApplicationErrorMessage
    {

        /// <summary>
        /// Gets assigned error message from the application's reference.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string GetErrorMessage(ApplicationErrorMessages message)
        {
            string errorMessage = "";
           
            try
            {
                var path = ConfigurationManager.AppSettings["ErrorMessages"];
                var xml = XElement.Load(HttpContext.Current.Server.MapPath(path));

                var query = from errorMessages in xml.Elements("errormessage")
                            where ((string)errorMessages.Element("key")) == Convert.ToInt32(message).ToString()
                            select errorMessages;

                if (query.Any()) errorMessage = (string)query.First().Element("message");
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("DukeNUS.FacilitiesPlanning.FPSystem.ApplicationErrorMessage", string.Format("Message: {0}\nStack Trace: {1}", ex.Message, ex.StackTrace), EventLogEntryType.Error);
            }

            return errorMessage;
        }

    }
}